<template>
  <div id="order_table">
    <div class="order_table_header">
      <el-table
        :data="orderList"
        stripe
        style="width: 100%"
      >
        <el-table-column
          prop="id"
          label="订单编号"
          width="180"
          align="center"
        />
        <el-table-column
          prop="orderTime"
          label="下单时间"
          width="180"
          align="center"
        />
        <el-table-column
          prop="total"
          label="总价"
          width="180"
          align="center"
        />
        <el-table-column
          prop="status"
          label="状态"
          width="180"
          align="center"
        />
        <el-table-column
          prop="customerId"
          label="顾客ID"
          width="180"
          align="center"
        />
        <el-table-column
          label="操作"
          align="center"
        >
          <template slot-scope="scope">
            <el-button type="text" @click="orderdetails(scope.row.id)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="order_table_footer">
      <div class="block">
        <el-pagination
          :current-page.sync="currentPage1"
          :page-size="5"
          layout="total, prev, pager, next"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>
<script>
import qs from 'querystring'

export default {
  data() {
    return {
      orders: [],
      currentPage1: 1,
      total: 0,
      pageSize: 6
    }
  },
  computed: {
    orderList() {
      this.orders.forEach((item) => {
        item.orderTime = new Date(item.orderTime).toLocaleDateString()
      })
      return this.orders
    }
  },
  created() {
    this.findAllorder()
  },
  methods: {
    // 给详情按钮绑定事件
    orderdetails(id) {
      this.$router.push({ path: '/orderdetails/index', query: { id: id }})
      console.log(id)
    },

    // 封装分页显示订单数据
    findAllorder(name) {
      if (name == '所有订单') {
        var data = {
          page: 1,
          pageSize: 6
        }
      } else {
        var data = {
          page: this.currentPage1,
          pageSize: this.pageSize,
          status: name
        }
      }

      this.$axios.post('/order/queryPage', qs.stringify(data)).then(response => {
        console.log(response.data.data.list)
        this.orders = response.data.data.list
        this.total = response.data.data.total
      })
    },

    // 分页选项
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      this.currentPage1 = val,
      this.findAllorder()
    }
  }
}
</script>
<style lang="scss" scoped>

</style>
